Skip to main content

as a

Summary

Evaluates value and converts it to the specified type, throwing an error if conversion is not possible.

Syntax

<value> as a[n] {boolean | integer | number | string | name | binary string | array | sequence | dictionary}

Description

Use the as a operator to convert data to a specific type using similar rules that the engine uses when values are passed to builtin commands and functions.

To convert to a boolean, the value must either already be a boolean value, or compare caselessly as true or false.

To convert to an integer, the value must either already be an integer value, or be is an integer.

To convert to a number, the value must either already be a number value, or be is a number.

To convert to a string, name or binary string, the value must not be a non-empty array (all other values have a string-like representation).

To convert to an array, the value must either already be an array, or otherwise be empty.

To convert to a sequence, the value must either be an array which is a sequence, or otherwise be empty.

To convert to a dictionary, the value must either be an array, or otherwise be empty.

If conversion succeeds, then the resulting value will have a specific strict type depending on the type requested:

  • boolean: The resulting value is strictly a boolean.
  • integer: The resulting value is strictly an integer if it is in the range -(2^31) to (2^31), otherwise the value is strictly a real.
  • number: The resulting value is strictly a number.
  • string: The resulting value is strictly a string.
  • name: The resulting value is strictly a name.
  • binary string: The resulting value is strictly a binary string.
  • array: The resulting value is strictly an array.
  • sequence: The resulting value is strictly a sequence.
  • dictionary: The resulting value is strictly a dictionary.

The as an array operator removes any sequence or dictionary mark which has been applied previously to an array using the corresponding as a operator.

Important

When a command has a clause which is prefixed by as (e.g. answer prompt as sheet), the as a operator can only be used when grouped using parentheses (e.g. answer (prompt as a string) as sheet). This is also true when using as a in a chunk expression.

note

These operators are currently only useful in contexts where the current internal representation of a value must be of a specific kind (strict type) which typically only occurs when manipulating data for export to a format for consumption by other systems. For example, JsonExport uses this information to determine whether to emit a null, boolean or number token rather than a string token.

Parameters

NameTypeDescription

value

Any source of value.

Examples

pi as an integer -- evaluates to 3
"foo" as a number -- throws an error
"bar" as a boolean -- throws an error
get empty as an array
put empty is strictly an array -- evaluates to true
("10.0" as a number) is strictly ("10" as a number) -- evaluates to true
("10.0" as a string) is strictly ("10" as a string) -- evaluates to false
("FOO" as a string) is strictly ("foo" as a string) -- evaluates to false
get [ 1, 2 ] as a dictionary
put it is strictly a sequence -- evaluates to false
get it as an array
put it is strictly a sequence -- evaluates to true

glossary: array, boolean, evaluate, integer, logical, operator, string, value

handler: JsonExport

keyword: array, integer

operator: is a, is not a, is strictly a, is not strictly a, is strictly

Compatibility and Support

Introduced

LiveCode Create 1.0

OS

mac

windows

linux

ios

android

web

Platforms

desktop

server

mobile

Thank you for your feedback!

Was this page helpful?